import $ from '@/common'
import BaseStore from 'store/lib/base'
//相对于第一版，只在使用时才调用store组装方法，加快了初始化方法，并且简化store的定义
function extendStore(me, base) {
  //对于，没有初始化的store基类，需要先组装完毕才能与当前store混合
  if (!base._build) {
    createStore(base)
  }
  //#WARN: 此处暂未处理循环依赖的情况，设置需注意
  return $.setObj(me, {
    actions: base.actions,
    getters: base.getters,
    mutations: base.mutations,
    state: $.copyJson(base.state)
  })
}
export default function createStore(store) {
  var entity = {}
  if (!store._build && store !== BaseStore) {
    store.namespaced = true
    if (store.extends) {
      extendStore(store, store.extends)
    } else {
      extendStore(store, BaseStore)
    }
    if (store.mixins) {
      for (let item of store.mixins) {
        extendStore(store, item)
      }
    }
    store._build = true
  }
  entity = {
    ...store
  }
  return entity
}
